<?php

declare(strict_types=1);

namespace app\admin\controller;

use think\facade\Db;
use think\facade\Request;

class Order
{
    /**
     * 用户订单列表
     */
    public function list()
    {
        $query = Request::param();
        $page_size = isset($query['page']) ? $query['page'] : 1;
        $page_limit = isset($query['page_size']) ? $query['page_size'] : 10;
        $page_size = ($page_size - 1) * $page_limit;
        // $where['pay_status'] = isset($query['pay_status']) ? $query['pay_status'] : '';
        $query['pay_status'] = isset($query['pay_status']) ? $query['pay_status'] : 2;
        if ($query['pay_status'] == 2) {
            $where['pay_status'] = 0 or 1;
        } else {
            $where['pay_status'] = $query['pay_status'];
        }
        $total = Db::name('order')->where($where)->count();
        $field = 'id,uid,total,order_sn,pay_status,create_time,update_time';
        $order = Db::name('order')->where($where)->field($field)->order('id desc')->limit((int)$page_size, (int)$page_limit)->select()->toArray();
        foreach ($order as $k => $v) {
            $user_data = Db::name('user')->where('uid', $v['uid'])->find();
            $order[$k]["nickname"] = userTextDecode($user_data['nickname']);
            $order[$k]["avatarUrl"] = $user_data['avatarUrl'];
        }
        $data = [
            'data' => $order,
            'total' => $total
        ];
        return jsonData(1, '查询成功', $data);
    }
}
